function [] = jpgcomp(file_prefix, save_dir, jpg_quality);

% JPGCOMP - writes compressed JPG files
%
%    JPGCOMP(FILE, DIR, QUALITY) reads JPG images having the filename
%    specification FILE (i.e., 'IMAGE*.jpg') in the subdirectory DIR
%    and writes another copy of each file with compression QUALITY.

% parameters

read_dir = 'c:\scm\pictures\original\';
write_dir = 'c:\scm\pictures\lowres\';

% validate arguments

if (nargin ~= 3)
   msgbox('Invalid number of arguments', 'JPGCOMP Error', 'warn');
   return
elseif (~ischar(file_prefix))
   msgbox('FILE must be a scalar', 'JPGCOMP Error', 'warn');
   return
elseif (~ischar(save_dir))
   msgbox('DIR must be a scalar', 'JPGCOMP Error', 'warn');
   return
elseif (~exist([write_dir save_dir], 'dir'))
   msgbox('DIR does not exist', 'JPGCOMP Error', 'warn');
   return
elseif (~isscalar(jpg_quality))
   msgbox('QUALITY must be a scalar', 'JPGCOMP Error', 'warn');
   return
end

% loop through files in list

sdir = dir([read_dir save_dir '\' file_prefix]);
h_wait = waitbar(0, 'Processing files ...');
for i = 1 : length(sdir)
   a = imread([read_dir save_dir '\' sdir(i).name], 'jpg');
   imwrite(a, [write_dir save_dir '\' sdir(i).name], 'jpg', 'Quality', jpg_quality);
   waitbar(i / length(sdir));
end
close(h_wait);
return
